package com.fast.admin.controller.common;

import com.fast.framework.core.AjaxResult;
import com.fast.framework.core.domain.User;
import com.fast.framework.core.model.LoginBody;
import com.fast.framework.core.service.ILoginService;
import com.fast.framework.enums.JwtEnum;
import com.fast.framework.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "登录验证API")
@RestController
public class LoginController {

    @Resource
    private ILoginService loginService;
    private static final String tokenHead = JwtEnum.tokenHead.getValue();

    @ApiOperation(value = "登陆接口")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody);
        ajax.put("token", token);
        ajax.put("tokenHead", tokenHead);
        return ajax;
    }

    @ApiOperation(value = "用户信息")
    @PostMapping("/getInfo")
    public AjaxResult getInfo() {
        User user = SecurityUtils.getLoginUser();
        user.setPassword("");
        return AjaxResult.success(user);
    }
}
